//
//  LoginView.m
//  Safflower
//
//  Created by 袁亮 on 2016/11/28.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "LoginView.h"

@interface LoginView ()

@property (nonatomic, strong) UITextField *phone_field;
@property (nonatomic, strong) UITextField *password_field;

@property (nonatomic, strong) UIButton *login_button;
@property (nonatomic, strong) UIButton *regist_button;
@property (nonatomic, strong) UIButton *forget_button;

@end

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeLoginView];
        
    }
    return self;
}

- (void) makeLoginView
{
    
    

    UIView *passwordView = [UIView new];
    passwordView.layer.masksToBounds = YES;
    passwordView.layer.borderWidth = 1;
    passwordView.layer.cornerRadius = 10;
    passwordView.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:passwordView];
    [passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3, 36));
    }];
    
    _password_field = [UITextField new];
    _password_field.placeholder = @"请输入密码";
    _password_field.secureTextEntry = YES;
    _password_field.font = [UIFont systemFontOfSize:17.0f];
    [passwordView addSubview:_password_field];
    [_password_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    
    UIView *phoneView = [UIView new];
    phoneView.layer.masksToBounds = YES;
    phoneView.layer.borderWidth = 1;
    phoneView.layer.cornerRadius = 10;
    phoneView.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:phoneView];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(passwordView.mas_top).mas_offset(-20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3, 36));
        make.centerX.mas_equalTo(self);
    }];
    
    _phone_field = [UITextField new];
    _phone_field.placeholder = @"请输入手机号码";
    _phone_field.keyboardType = UIKeyboardTypeNumberPad;
    _phone_field.font = [UIFont systemFontOfSize:17.0f];
    [phoneView addSubview:_phone_field];
    [_phone_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    CGFloat logoY = ((SCREEN_HEIGHT - 148) / 2 - 40) / 2;
    
    UIImageView *logo_image_view = [UIImageView new];
    logo_image_view.image = [UIImage imageNamed:@"login_top_logo_image.png"];
    [self addSubview:logo_image_view];
    [logo_image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(phoneView.mas_top).mas_offset(-logoY);
        make.size.mas_equalTo(CGSizeMake(160, 40));
        make.centerX.mas_equalTo(self);
    }];
    
    
    _login_button = [UIButton new];
    _login_button.layer.masksToBounds = YES;
    _login_button.layer.borderWidth = 1;
    _login_button.layer.cornerRadius = 10;
    _login_button.layer.borderColor = [[UIColor blackColor] CGColor];
    [_login_button setTitle:@"登陆" forState:UIControlStateNormal];
    [_login_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_login_button addTarget:self action:@selector(clickLoginButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_login_button];
    [_login_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordView.mas_bottom).mas_offset(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3, 40));
        make.centerX.mas_equalTo(self);
    }];
    
    _regist_button = [UIButton new];
    [_regist_button setTitle:@"注册" forState:UIControlStateNormal];
    [_regist_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_regist_button addTarget:self action:@selector(clickRegistButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_regist_button];
    [_regist_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_login_button.mas_bottom).mas_equalTo(10);
        make.left.mas_equalTo(_login_button.mas_left).mas_equalTo(5);
        make.size.mas_equalTo(CGSizeMake(50, 20));
    }];
    
    _forget_button = [UIButton new];
    [_forget_button setTitle:@"忘记密码" forState:UIControlStateNormal];
    [_forget_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:_forget_button];
    [_forget_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_login_button.mas_bottom).mas_equalTo(10);
        make.right.mas_equalTo(_login_button.mas_right).mas_equalTo(-5);
        make.size.mas_equalTo(CGSizeMake(75, 20));
    }];
}


- (void) clickLoginButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(clickLoginBtnWithPhone:password:)]) {
        [self.delegate clickLoginBtnWithPhone:_phone_field.text password:_password_field.text];
    }
}

- (void) clickRegistButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(clickRegistBtn:)]) {
        [self.delegate clickRegistBtn:sender];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_phone_field resignFirstResponder];
    [_password_field resignFirstResponder];
}

@end
